package com.ihelp.rest.mis.platform.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.ihelp.rest.base.hibernate4.BaseHibernateDao;
import com.ihelp.rest.base.po.Page;
import com.ihelp.rest.mis.pojo.Admdivision;

@Repository
public class AdmdivisionDao extends BaseHibernateDao<Admdivision,Long>{
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	/**
	 * 查询区划分页列表
	 * @param queryCondition
	 * @param start
	 * @param pageSize
	 * @return
	 */
	public Page<Admdivision> queryAdmdivPage(Admdivision queryCondition,int start,int pageSize,String sort, String order){
		StringBuilder hql = new StringBuilder();
		ArrayList<Object> list = new ArrayList<Object>();
		hql.append("from Admdivision t where 1=1 ");
		if(StringUtils.isNotBlank(queryCondition.getParentcode())){
			hql.append(" and t.parentcode=?");
			list.add(queryCondition.getParentcode());
		}
		if(StringUtils.isNotBlank(queryCondition.getAdcode())){
			hql.append(" and t.adcode=?");
			list.add(queryCondition.getAdcode());
		}
		if(StringUtils.isNotBlank(queryCondition.getAdname())){
			hql.append(" and t.adname like ?");
			list.add("%"+queryCondition.getAdname()+"%");
		}
		if(StringUtils.isNotBlank(sort))
			hql.append(" order by ").append(sort).append(" ").append(order);
		return this.page(hql.toString(), start, pageSize, (Object[])list.toArray());
	}
	

	/**
	 * 根据parentcode查询区划List
	 * @param parentcode
	 * @return
	 */
	public List<Admdivision> queryAdmdivByParentCode(String parentcode){
		return this.list("from Admdivision where parentcode=? order by adcode", parentcode);
	}
	
	
	public Admdivision getAdmdivByCode(String adcode){
		return this.unique("from Admdivision where adcode=?", adcode);
	}
	
	public Admdivision getAdmdivById(Long id){
		return this.unique("from Admdivision where id=?", id);
	}
	
	public long validCode(Admdivision admdiv){
		long lRet = 0;
		String sql = "";
		if(StringUtils.isNotBlank(admdiv.getAdcode())){
			if(admdiv.getId()==null)
				sql = "select count(id) as l_count from Admdivision t where t.adcode='"+admdiv.getAdcode()+"'";
			else
				sql = "select count(id) as l_count from Admdivision t where t.adcode='"+admdiv.getAdcode()+"' and t.id<>'"+admdiv.getId()+"'";
			List<Map<String, Object>> result = this.jdbcTemplate.queryForList(sql);
			for (Map<String, Object> one : result) {
				lRet = Long.valueOf(one.get("l_count").toString());
			}
		}
		return lRet;
	}
	
	public long getSubAdNum(String id){
		long lRet = 0;
		String sql = "select count(id) as l_count from Admdivision t where t.parentcode in(select adcode from Admdivision where id="+id+")";
		List<Map<String, Object>> result = this.jdbcTemplate.queryForList(sql);
		for (Map<String, Object> one : result) {
			lRet = Long.valueOf(one.get("l_count").toString());
		}
		return lRet;
	}
}
